package com.android.launcher3;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

/**
 * Folder which contains applications or shortcuts chosen by the user.
 */
public class UserFolder extends Folder implements DropTarget {
	/**
	 * 
	 * @param context
	 * @param attrs
	 */
	public UserFolder(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public boolean acceptDrop(final DragSource source, final int x,
			final int y, final int xOffset, final int yOffset,
			final Object dragInfo) {
		final ItemInfo item = (ItemInfo) dragInfo;
		final int itemType = item.itemType;

		return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
				&& item.container != mInfo.id;
	}

	public Rect estimateDropLocation(final DragSource source, final int x,
			final int y, final int xOffset, final int yOffset,
			final Object dragInfo, final Rect recycle) {
		return null;
	}

	public void onDragEnter(final DragSource source, final int x, final int y,
			final int xOffset, final int yOffset, final Object dragInfo) {
	}

	public void onDragExit(final DragSource source, final int x, final int y,
			final int xOffset, final int yOffset, final Object dragInfo) {
	}

	public void onDragOver(final DragSource source, final int x, final int y,
			final int xOffset, final int yOffset, final Object dragInfo) {
	}

	public void onDrop(final DragSource source, final int x, final int y,
			final int xOffset, final int yOffset, final Object dragInfo) {
		ItemInfo localItemInfo = (ItemInfo) dragInfo;
		ShortcutInfo localShortcutInfo;

		if ((localItemInfo instanceof ApplicationInfo)) {
			localShortcutInfo = ((ApplicationInfo) localItemInfo)
					.makeShortcut();
		} else {
			localShortcutInfo = (ShortcutInfo) localItemInfo;
		}

		((ShortcutsAdapter) this.mContent.getAdapter()).add(localShortcutInfo);

		Launcher localLauncher = this.mLauncher;
		LauncherModel.addOrMoveItemInDatabase(localLauncher, localShortcutInfo,
				this.mInfo.id, 0, 0, 0);
	}

	@Override
	public void onDropCompleted(final View target, final boolean success,
			final Object dragInfo) {
		if (success) {
			ShortcutsAdapter localShortcutsAdapter = (ShortcutsAdapter) this.mContent
					.getAdapter();
			localShortcutsAdapter.remove(this.mDragItem);
		}
	}

	/**
	 * Creates a new UserFolder, inflated from R.layout.user_folder.
	 * 
	 * @param context
	 *            The application's context.
	 * 
	 * @return A new UserFolder.
	 */
	static UserFolder fromXml(final Context context) {
		return (UserFolder) LayoutInflater.from(context).inflate(
				R.layout.user_folder, null);
	}

	void bind(final FolderInfo info) {
		super.bind(info);

		Context localContext = this.mContext;
		ArrayList localArrayList = ((UserFolderInfo) info).contents;
		ShortcutsAdapter localShortcutsAdapter = new ShortcutsAdapter(
				localContext, localArrayList);
		setContentAdapter(localShortcutsAdapter);
	}

	@Override
	void onOpen() {
		super.onOpen();
		requestFocus();
	}
}
